A method for defragging a memory of an IC card, the IC card comprising a plurality of files stored in memory portions and including respective links to one or more other files, may compact free memory portions deriving from a release of the memory portions when the corresponding files are deleted or moved. More particularly, a fragmentation of the memory occurs when the IC card operating system does not allocate enough contiguous free memory portions to store a file as a unit but instead puts parts of the file in gaps, i.e. in free memory portions between other files. Fragmentation also occurs when such gaps or free memory portions are too small to store files.
A known method to defrag a memory may include moving into a swap area a plurality of files which cause fragmentation of memory, releasing the corresponding memory portions, compacting them with neighboring free memory portions, and finally copying the files from the swap area in contiguous memory portions of the memory.
A drawback of this method may be that it involves too many IC card resources because it uses a swap area for temporary storage of the files and several writing operations from the memory portions into the swap are and vice versa, thus involving too much time, memory space, and processing resources to defragment the memory. Moreover, the method may recalculate the links from the files of the memory portions not involved in the defragmentation towards the moved files, as well as the links between the moved files themselves and from the moved files to the files not involved in the defragmentation.
This drawback is apparent when the memory of the IC card uses a tree structure file system including a master file MF or root node, and a plurality of children nodes, also indicated as directory nodes DF, elementary files EF and local object LO. More particularly, the master file MF links every children node, i.e. directory nodes DF and/or elementary files EF and/or local object LO, and each directory node DF may link further children nodes, i.e. further directory nodes DF and/or elementary files EF and/or local object LO. Thus, when the method moves children nodes from the respective memory portions to the swap area and when it moves back such children nodes from the swap area to the memory portions, it may recalculate all the links from the respective parent nodes and to children nodes.